package com.magicsoft.share.ui.share;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.presenter.share.CommentWritePresenter;
import com.magicsoft.share.utils.KeyboardUtils;
import com.magicsoft.share.utils.ToastUtils;
import com.magicsoft.share.view.share.ICommentWriteView;

import butterknife.BindView;
import butterknife.OnClick;

public class CommentWriteActivity extends BaseActivity<ICommentWriteView, CommentWritePresenter> implements ICommentWriteView {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.edt_comment)
    EditText edtComment;
    @BindView(R.id.tv_num)
    TextView tvNum;
    private String commentStr;
    private long articleId;


    @Override
    public CommentWritePresenter initPresenter() {
        return new CommentWritePresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_comment_write;
    }

    @Override
    public void initData() {

        articleId = getIntent().getLongExtra("articleId", -1);

    }

    @Override
    public void initView() {
        tvRight.setText("确定");
        edtComment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int i = 300 - editable.length();
                tvNum.setText("您还可以输入" + i + "字");
            }
        });

    }

    @Override
    public void requestData() {

    }


    @OnClick({R.id.ibtn_left, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_left:
                finish();
                break;
            case R.id.tv_right:
                confirmComment();
                break;
            default:
                break;
        }
    }

    private void confirmComment() {

        int length = edtComment.length();
        if (length == 0) {
            ToastUtils.showToast(getApplicationContext(), "至少输入一个字哦");
        } else {

            mPresenter.commentWrite(articleId,edtComment.getText().toString());
        }
    }

    @Override
    public void commentWriteSuccess() {
        setResult(RESULT_OK);
        ToastUtils.showToast(this, "评论成功");
        KeyboardUtils.closeKeybord(edtComment, this);
        finish();
    }
}
